package com.jt.assistant.ui

import android.annotation.SuppressLint
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import coil.load
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.reflect.TypeToken
import com.jt.assistant.R
import com.jt.assistant.adapter.HomeAdapter
import com.jt.assistant.adapter.OrderChooseAdapter
import com.jt.assistant.adapter.WaybillListAdapter
import com.jt.assistant.base.GlobalConfig
import com.jt.assistant.base.GlobalConfig.ATYPE
import com.jt.assistant.base.GlobalConfig.LICENSE_NAME
import com.jt.assistant.base.GlobalConfig.RELATION_ID
import com.jt.assistant.bean.HomeBean
import com.jt.assistant.bean.Param
import com.jt.assistant.bean.WaybillCityBean
import com.jt.assistant.bean.Waybills
import com.jt.assistant.viewmodel.HomeViewModel
import com.yzp.mvvmlibrary.base.BaseActivity
import kotlinx.android.synthetic.main.activity_order_selection.*

/**
 * 单号选择
 */
class OrderSelectionActivity : BaseActivity<HomeViewModel>() {
    private val mAdapter by lazy {
        OrderChooseAdapter(
            ArrayList<Waybills>()
        )
    }

    override fun layoutId(): Int = R.layout.activity_order_selection

    override fun initView(savedInstanceState: Bundle?) {
        with(rv_dialog) {
            layoutManager = LinearLayoutManager(context)
            adapter = mAdapter
        }
    }

    override fun initData() {
        mAdapter.data.addAll(
            GsonUtils.fromJson<java.util.ArrayList<Waybills>>(
                intent.getStringExtra("waybillList"),
                object : TypeToken<java.util.ArrayList<Waybills>>() {}.type
            )
        )
        mAdapter.notifyDataSetChanged()
        jd_order_tv.text = "一键勾选(0/${mAdapter.data.size})"
    }

    @SuppressLint("SetTextI18n")
    override fun initListener() {
        rl_oneKey.setOnClickListener {
            val isAllChoose = checkAllChoose()
            mAdapter.data.forEach {
                it.isChoose = !isAllChoose
            }
            mAdapter.notifyDataSetChanged()
            if (!isAllChoose) jd_check_iv.load(R.mipmap.icon_checked) else jd_check_iv.load(R.mipmap.icon_uncheck)
            jd_order_tv.text = "一键勾选(${checkNum()}/${mAdapter.data.size})"
        }

        mAdapter.setOnItemClickListener { adapter, view, position ->
            mAdapter.data[position].isChoose = !mAdapter.data[position].isChoose
            mAdapter.notifyDataSetChanged()
            if (checkAllChoose()) jd_check_iv.load(R.mipmap.icon_checked) else jd_check_iv.load(R.mipmap.icon_uncheck)
            jd_order_tv.text = "一键勾选(${checkNum()}/${mAdapter.data.size})"
        }


        bt_commit.setOnClickListener {
            if (checkCode().isEmpty()) {
                ToastUtils.showShort("请选择单号")
                return@setOnClickListener
            }
            startActivity(
                Intent(
                    this@OrderSelectionActivity,
                    DocumentDetailsActivity::class.java
                ).apply {
                    putExtra("waybillList", GsonUtils.toJson(checkCode()))
                    putExtra(RELATION_ID, intent.getStringExtra(RELATION_ID).toString())
                    putExtra(ATYPE, intent.getStringExtra(ATYPE).toString())
                    putExtra(LICENSE_NAME, intent.getStringExtra(LICENSE_NAME).toString())

                })
            finish()
        }
    }

    private fun checkAllChoose(): Boolean {
        mAdapter.data.forEach {
            if (!it.isChoose) {
                return false
            }
        }
        return true
    }

    private fun checkNum(): Int {
        var num = 0
        mAdapter.data.forEach {
            if (it.isChoose) {
                num++
            }
        }
        return num
    }

    private fun checkCode(): ArrayList<Waybills> {
        val waybillList = ArrayList<Waybills>()
        mAdapter.data.forEach {
            if (it.isChoose) {
                waybillList.add(it)
            }
        }
        return waybillList
    }


}